<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="96px">
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="图号" prop="imageNumber">
        <el-input v-model="queryParams.imageNumber" placeholder="请输入图号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="物料属性" prop="materialProperty" size="48px">
        <el-select v-model="queryParams.materialProperty" placeholder="请选择物料属性" clearable style="width: 108px;">
          <el-option v-for="dict in quality_material_prop" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="采购模式" prop="purchasingMode" size="48px">
        <el-select v-model="queryParams.purchasingMode" placeholder="请选择采购模式" clearable style="width: 108px;">
          <el-option v-for="dict in quality_material_purchasing" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户编号" prop="customId">
        <el-input v-model="queryParams.customId" placeholder="请输入客户提供编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="供应方编号" prop="supplierId">
        <el-input v-model="queryParams.supplierId" placeholder="请输入供应方提供编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="默认库存" prop="materiaQuantity">
        <el-input v-model="queryParams.materiaQuantity" placeholder="请输入默认库存" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="采购人员名称" prop="purchasingUser">
        <el-input v-model="queryParams.purchasingUser" placeholder="请输入采购人员名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['quality:material:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['quality:material:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['quality:material:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['quality:material:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编号" align="center" prop="materialId" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="物料描述" align="center" prop="materialDescription" />
      <!-- <el-table-column label="图号" align="center" prop="imageNumber" /> -->
      <el-table-column label="物料属性" align="center" prop="materialProperty">
        <template #default="scope">
          <dict-tag :options="quality_material_prop" :value="scope.row.materialProperty" />
        </template>
      </el-table-column>
      <el-table-column label="采购模式" align="center" prop="purchasingMode">
        <template #default="scope">
          <dict-tag :options="quality_material_purchasing" :value="scope.row.purchasingMode" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="客户编号" align="center" prop="customId" />
      <el-table-column label="供应方编号" align="center" prop="supplierId" /> -->
      <el-table-column label="默认库存" align="center" prop="materiaQuantity" />
      <!-- <el-table-column label="图片展示" align="center" prop="imageUrl" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imageUrl" :width="50" :height="50" />
        </template>
      </el-table-column> -->
      <el-table-column label="采购人员名称" align="center" prop="purchasingUser" />
      <el-table-column label="参考价格" align="center" prop="refferencePrice" />
      <el-table-column label="推荐价格" align="center" prop="recommendPrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['quality:material:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['quality:material:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改物料管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="materialRef" :model="form" label-width="96px" :rules="rule">
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料描述" prop="materialDescription">
          <el-input v-model="form.materialDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="图号" prop="imageNumber">
          <el-input v-model="form.imageNumber" placeholder="请输入图号" />
        </el-form-item> -->
        <el-form-item label="物料属性" prop="materialProperty">
          <el-select v-model="form.materialProperty" placeholder="请选择物料属性">
            <el-option v-for="dict in quality_material_prop" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采购模式" prop="purchasingMode">
          <el-select v-model="form.purchasingMode" placeholder="请选择采购模式">
            <el-option v-for="dict in quality_material_purchasing" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="客户编号" prop="customId">
          <el-input v-model="form.customId" placeholder="请输入客户提供编号" />
        </el-form-item>
        <el-form-item label="供应方编号" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应方提供编号" />
        </el-form-item> -->
        <el-form-item label="默认库存" prop="materiaQuantity">
          <el-input v-model="form.materiaQuantity" placeholder="请输入默认库存" />
        </el-form-item>
        <!-- <el-form-item label="图片路径" prop="imageUrl">
          <image-upload v-model="form.imageUrl" />
        </el-form-item> -->
        <el-form-item label="采购人员名称" prop="purchasingUser">
          <el-input v-model="form.purchasingUser" placeholder="请输入采购人员名称" />
        </el-form-item>
        <el-form-item label="参考价格" prop="refferencePrice">
          <el-input v-model="form.refferencePrice" placeholder="请输入参考价格" />
        </el-form-item>
        <el-form-item label="推荐价格" prop="recommendPrice">
          <el-input v-model="form.recommendPrice" placeholder="请输入推荐价格" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>



  </div>
</template>

<script setup name="Material">
import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial } from "@/api/quality/material";
import { getToken } from "@/utils/auth";
import { reactive } from "vue";
const { proxy } = getCurrentInstance();
const { quality_material_prop, quality_material_purchasing } = proxy.useDict('quality_material_prop', 'quality_material_purchasing');

const materialList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const rule = reactive({
  materialName: [{ required: true, message: "输入不能为空", trigger: 'blur' }],
  materialDescription: [{ required: true, message: "输入不能为空", trigger: 'blur' }],
  supplierId: [{ required: true, message: "输入不能为空", trigger: 'blur' }, { type: 'number', message: '需要整数', trigger: 'blur' }],
  customId: [{ required: true, message: "输入不能为空", trigger: 'blur' }, { type: 'number', message: '需要整数', trigger: 'blur' }]
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    materialName: null,
    materialDescription: null,
    imageNumber: null,
    materialProperty: null,
    purchasingMode: null,
    customId: null,
    supplierId: null,
    materiaQuantity: null,
    purchasingUser: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

const uploadDialog = ref(false);


const upload = reactive({
  // 是否显示弹出层（用户导入）
  open: false,
  // 弹出层标题（用户导入）
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/quality/material/importData"
});
/** 查询物料管理列表 */
function getList() {
  loading.value = true;
  listMaterial(queryParams.value).then(response => {
    materialList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    materialId: null,
    materialName: null,
    materialDescription: null,
    imageNumber: null,
    materialProperty: null,
    purchasingMode: null,
    customId: null,
    supplierId: null,
    materiaQuantity: null,
    createTime: null,
    createBy: null,
    imageUrl: null,
    purchasingUser: null,
    refferencePrice: null,
    recommendPrice: null
  };
  proxy.resetForm("materialRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.materialId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加物料管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _materialId = row.materialId || ids.value
  getMaterial(_materialId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改物料管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["materialRef"].validate(valid => {
    if (valid) {
      if (form.value.materialId != null) {
        updateMaterial(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMaterial(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _materialIds = row.materialId || ids.value;
  proxy.$modal.confirm('是否确认删除物料管理编号为"' + _materialIds + '"的数据项？').then(function () {
    return delMaterial(_materialIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('quality/material/export', {
    ...queryParams.value
  }, `material_${new Date().getTime()}.xlsx`)
}

function importTemplate() {
  proxy.download("quality/material/importTemplate", {
  }, `user_template_${new Date().getTime()}.xlsx`);
}

getList();
</script>
